<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreCouponRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        $couponId = $this->route('coupon') ? $this->route('coupon')->id : null;

        return [
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:50|unique:coupons,code,' . $couponId,
            'type' => 'required|in:fixed,percent,shipping,full_reduction',
            'value' => 'required|numeric|min:0',
            'min_purchase' => 'nullable|numeric|min:0',
            'max_discount' => 'nullable|numeric|min:0',
            'total_quantity' => 'nullable|integer|min:0',
            'per_user_limit' => 'nullable|integer|min:0',
            'valid_days' => 'nullable|integer|min:1|max:365',
            'start_time' => 'nullable|date',
            'end_time' => 'nullable|date|after:start_time',
            'applicable_type' => 'required|in:all,category,product',
            'applicable_ids' => 'nullable|array',
            'applicable_ids.*' => 'integer|min:1',
            'stackable' => 'boolean',
            'description' => 'nullable|string',
            'usage_note' => 'nullable|string|max:500',
            'is_active' => 'boolean',
            'is_new_user_only' => 'boolean',
            'sort_order' => 'nullable|integer|min:0',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => '优惠券名称不能为空',
            'name.max' => '优惠券名称不能超过255个字符',
            'code.required' => '优惠券代码不能为空',
            'code.unique' => '优惠券代码已存在',
            'code.max' => '优惠券代码不能超过50个字符',
            'type.required' => '请选择优惠券类型',
            'type.in' => '优惠券类型无效',
            'value.required' => '优惠金额不能为空',
            'value.numeric' => '优惠金额必须是数字',
            'value.min' => '优惠金额不能小于0',
            'min_purchase.numeric' => '最低消费金额必须是数字',
            'min_purchase.min' => '最低消费金额不能小于0',
            'max_discount.numeric' => '最大优惠金额必须是数字',
            'max_discount.min' => '最大优惠金额不能小于0',
            'total_quantity.integer' => '发放总数量必须是整数',
            'total_quantity.min' => '发放总数量不能小于0',
            'per_user_limit.integer' => '每人限领次数必须是整数',
            'per_user_limit.min' => '每人限领次数不能小于0',
            'valid_days.integer' => '有效天数必须是整数',
            'valid_days.min' => '有效天数不能小于1天',
            'valid_days.max' => '有效天数不能超过365天',
            'start_time.date' => '开始时间格式不正确',
            'end_time.date' => '结束时间格式不正确',
            'end_time.after' => '结束时间必须晚于开始时间',
            'applicable_type.required' => '请选择适用范围',
            'applicable_type.in' => '适用范围无效',
            'applicable_ids.array' => '适用ID必须是数组',
            'applicable_ids.*.integer' => '适用ID必须是整数',
            'applicable_ids.*.min' => '适用ID必须大于0',
            'usage_note.max' => '使用说明不能超过500个字符',
            'sort_order.integer' => '排序必须是整数',
            'sort_order.min' => '排序不能小于0',
        ];
    }

    /**
     * Configure the validator instance.
     */
    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            // 根据类型验证特定字段
            $type = $this->input('type');
            $value = $this->input('value');

            if ($type === 'percent' && $value > 10) {
                $validator->errors()->add('value', '折扣券的折扣不能超过10折');
            }

            if ($type === 'percent' && $value < 1) {
                $validator->errors()->add('value', '折扣券的折扣不能小于1折');
            }

            // 验证适用ID
            if ($this->input('applicable_type') !== 'all' && empty($this->input('applicable_ids'))) {
                $validator->errors()->add('applicable_ids', '请选择适用的分类或商品');
            }

            // 验证时间设置
            if ($this->input('valid_days') && $this->input('end_time')) {
                $validator->errors()->add('valid_days', '设置了固定结束时间时，不能同时设置有效天数');
            }

            if (!$this->input('valid_days') && !$this->input('end_time')) {
                $validator->errors()->add('valid_days', '请设置有效天数或固定结束时间');
            }
        });
    }
}